#!/bin/bash

mkdir -p shared/server-data
mkdir -p shared/agent-data
mkdir -p test/before-server-upgrade
mkdir -p test/after-server-upgrade
mkdir -p test/after-agent-upgrade

"${ROOTDIR}/setup/x509pop/setup.sh" conf/server conf/agent

make-service() {
    local _registry=$1
    local _version=$2
cat <<EOF >> docker-compose.yaml
  spire-server-${_version}:
    container_name: spire-server-${_version}
    image: ${_registry}spire-server:${_version}
    hostname: spire-server
    user: "${UID}"
    healthcheck:
      # TODO: Use default socket path in 1.7.0
      test: ["CMD", "/opt/spire/bin/spire-server", "healthcheck", "-socketPath", "/opt/spire/data/server/socket/api.sock"]
      interval: 1s
      timeout: 3s
      retries: 15
    networks:
      our-network:
        aliases:
          - spire-server
    volumes:
      - ./shared/server-data:/opt/spire/data
      - ./conf/server:/opt/spire/conf/server
    command: ["-config", "/opt/spire/conf/server/server.conf"]
  spire-agent-${_version}:
    container_name: spire-agent-${_version}
    image: ${_registry}spire-agent:${_version}
    hostname: spire-agent
    user: "${UID}"
    healthcheck:
      # TODO: Use default socket path in 1.7.0
      test: ["CMD", "/opt/spire/bin/spire-agent", "healthcheck", "-socketPath", "/opt/spire/data/agent/socket/api.sock"]
      interval: 1s
      timeout: 3s
      retries: 15
    networks:
      - our-network
    volumes:
      - ./shared/agent-data:/opt/spire/data
      - ./conf/agent:/opt/spire/conf/agent
      - ./test:/opt/test
    command: ["-config", "/opt/spire/conf/agent/agent.conf"]
EOF
}

#
# Create the docker-compose.yaml with a spire-server and spire-agent for each
# version we want to test against the latest
#
cat <<EOF > docker-compose.yaml
networks:
  our-network: {}
services:
EOF

make-service "" latest-local
while read -r version; do
  make-service ghcr.io/spiffe/ "${version}"
done < versions.txt
